using System;
using System.Web.UI;
using umbraco;
using umbraco.cms.businesslogic.member;

namespace umbracoMemberControls
{
    /// <summary>
    ///		Summary description for umbGroupSignInOut.
    /// </summary>
    public partial class umbLogin : UserControl
    {
        private bool _logoutOnly = false;
        private string _loggedInText = "You're logged in as %%NAME%% - ";
        private string _signoutText = "Sign out";

        private bool  _useCookiesForAuthentication  = false;

        public bool  UseCookiesForAuthentication
        {
            get { return _useCookiesForAuthentication ; }
            set { _useCookiesForAuthentication  = value; }
        }
	

        public bool LogOutOnly
        {
            set { _logoutOnly = value; }
        }

        public string LoggedInText
        {
            set { _loggedInText = value; }
        }

        public string SignOutLinkText
        {
            set { _signoutText = value; }
        }

        public string UsernameCaption
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                    LiteralUsername.Text = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                else
                    LiteralUsername.Text = value;
            }
        }

        public string PasswordCaption
        {
            set
            {
                if (value.Length > 0 && value.Substring(0, 1) == "#")
                    LiteralPassword.Text = umbraco.library.GetDictionaryItem(value.Substring(1, value.Length - 1));
                else 
                    LiteralPassword.Text = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            signoutText.Text = _signoutText;
            if (_logoutOnly)
            {
                PanelSignIn.Visible = false;
                if (Member.GetCurrentMember() != null)
                {
                    updateUserName();
                    PanelSignOut.Visible = true;
                }
                else
                    PanelSignOut.Visible = false;
            }
            else
            {
                if (Member.GetCurrentMember() != null)
                {
                    updateUserName();
                    PanelSignIn.Visible = false;
                    PanelSignOut.Visible = true;
                }
                else
                {
                    PanelSignIn.Visible = true;
                    PanelSignOut.Visible = false;
                }
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }

        #endregion

        protected void ButtonCreate_Click(object sender, EventArgs e)
        {
            Member m = Member.GetMemberFromLoginNameAndPassword(TextBoxUserName.Text, TextBoxPassword.Text);
            if (m != null)
            {
                if (UseCookiesForAuthentication)
                    Member.AddMemberToCache(m);
                else
                    Member.AddMemberToCache(m, true, new TimeSpan(0,0,0));

                Response.Redirect(requestHandler.cleanUrl(), true);
            }
        }

        private void updateUserName()
        {
            LiteralLogout.Text = _loggedInText.Replace("%%NAME%%", Member.GetCurrentMember().Text);
        }

        protected void LinkButtonLogout_Click(object sender, EventArgs e)
        {
            Member.ClearMemberFromClient(Member.GetCurrentMember().Id);
            Response.Redirect(requestHandler.cleanUrl(), true);
        }
    }
}